<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    .router-link-active {
      background-color: red;
    }
  </style>
</head>

<body>
  <div id="app">
    <router-link tag="button" to="/home">首页</router-link>
    <router-link tag="button" to="/news">新闻页</router-link>
    <router-link tag="button" replace to="/about">关于我们</router-link>
    <hr>
    <keep-alive>
      <router-view v-if="$route.meta.keepAlive"></router-view>
    </keep-alive>
    <router-view v-if="!$route.meta.keepAlive"></router-view>
  </div>
  <script src="./vue.js"></script>
  <script src="./vue-router.js"></script>
  <script>
    // 定义好路由组件的options
    const Home = {
      name: 'Home',
      template: `
        <div>
          <h2>我是home页</h2>
        </div>
      `,
      mounted(){
        console.log('home组件初始化完成了');
      },
      beforeDestroy(){
        console.log('home组件马上要卸载');
      }
    }
    const News = {
      name: 'News',
      template: `
        <div>
          <h2>我是news页</h2>
        </div>
      `,
      mounted(){
        console.log('news组件初始化完成了');
      },
      beforeDestroy(){
        console.log('news组件马上要卸载');
      }
    }
    const About = {
      name: 'About',
      template: `
        <div>
          <h2>我是about页</h2>
        </div>
      `
    }
    const NotFound = {
      template: `
        <div>
          <h2>我是404页</h2>
        </div>
      `
    }
    const Login = {
      template: `
        <div>
          <h3>登录页</h3>  
          <button @click="doLogin">点击登陆</button>
        </div>
      `,
      methods: {
        doLogin() {
          // 模拟登陆成功的ajax
          setTimeout(() => {
            // 这是登陆成功后端返回token
            const token = 'dfehigj4rodfh3ei';
            localStorage.setItem('token', token);
            /* 
              跳转到 之前 被拦截的路由中
              需要： 在这里知道 从哪里路由判断没有登录 拦截到登录的

              判断：
                1 如果用户直接访问的登录 登录成功之后直接跳转到首页即可
                2 用户访问其他路由被拦截到 登录页 跳回原来页面
            */
            if (this.$route.query.from) {
              this.$router.replace(this.$route.query.from)
            } else {
              this.$router.replace('/home')
            }

          }, 1000)
        }
      }
    }
    const routes = [
      {
        path: '/',
        redirect: '/home'
      },
      {
        path: '/home',
        meta: {
          needLogin: false,
          keepAlive: true
        },
        component: Home
      },
      {
        path: '/news',
        meta: {
          needLogin: true,
          keepAlive: true
        },
        component: News
      },
      {
        path: '/about',
        meta: {
          needLogin: true,
          keepAlive: false
        },
        component: About
      },
      {
        path: '/login',
        meta: {
          needLogin: false,
          keepAlive: false
        },
        component: Login
      },
      {
        path: '*',
        component: NotFound
      }
    ]
    const router = new VueRouter({
      routes
    })
    
    const vm = new Vue({
      el: '#app',
      router
    })
  </script>
</body>

</html>